package com.weixin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weixin.config.WeiXinProperties;
import com.weixin.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/1517:14
 */
@Api("企业微信OA-汇报")
@RestController
@RequestMapping("/v1.0/wei-xin/cgi-bin/report")
public class ReportConyroller {
    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private ObjectMapper objectMapper;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("批量获取汇报记录单号")
    @GetMapping("/getCorpCheckInoption")
    public ResponseEntity getCorpCheckInoption(@RequestParam("startTime") Long startTime ,
                                               @RequestParam("endTime") Long endTime ,
                                               @RequestParam("cursor") Integer cursor ,
                                               @RequestParam("limit") Integer limit ) throws IOException {
        Map map= new HashMap<String, Object>();
        map.put("starttime",startTime);
        map.put("endtime",endTime);
        map.put("cursor",cursor);
        map.put("limit",limit);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/oa/journal/get_record_list?");
        String access_token = (String) redisUtil.get("access_token_report");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }

    private ResponseEntity resultMap(Map<String, Object> resultMap, Integer type, String key){
        for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
            if (entry.getKey().equals("errcode")){
                Integer integer = (Integer) resultMap.get("errcode");
                if (integer==0){
                    ResponseEntity responseEntity = type==0 ? new ResponseEntity(resultMap.get(key), HttpStatus.OK): new ResponseEntity(resultMap, HttpStatus.OK);
                    return responseEntity;
                }
            }
        }
        return new ResponseEntity(resultMap,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
